"""
    生成500个用户名和密码（8位数）
"""
import random
import string
"""
    生成500个用户名和密码（8位数）
"""
import random
import string


def generate_and_save_users(count=500, filename=r"C:\Users\suyujie\Desktop\测试数据\users.txt"):
    """
    生成用户名和密码并保存到CSV文件
    """
    users = []
    usernames = set()  # 用于存储已生成的用户名，避免重复

    while len(users) < count:
        # 生成用户名
        username_patterns = [
            lambda: f"user{random.randint(1000, 9999)}",
            lambda: f"{random.choice(['admin', 'member', 'client', 'customer'])}{random.randint(100, 9999)}",
            lambda: ''.join(random.choices(string.ascii_lowercase, k=random.randint(6, 10))) + str(
                random.randint(10, 99)),
            lambda: f"{random.choice(['dev', 'test', 'demo'])}_{random.randint(1000, 9999)}"
        ]

        username = random.choice(username_patterns)()

        # 检查用户名是否已存在，如果存在则重新生成
        if username in usernames:
            continue

        # 将用户名添加到集合中
        usernames.add(username)

        # 生成密码（确保包含大小写字母、数字和特殊字符）
        lowercase = random.choices(string.ascii_lowercase, k=random.randint(3, 5))
        uppercase = random.choices(string.ascii_uppercase, k=random.randint(2, 3))
        digits = random.choices(string.digits, k=random.randint(2, 3))

        password_chars = lowercase + uppercase + digits
        random.shuffle(password_chars)
        password = ''.join(password_chars)

        users.append([username, password])

    # 保存到user.txt文件，每行一条数据，且每个参数用;隔开
    with open(filename, "w") as f:
        for user in users:
            f.write(f"{user[0]};{user[1]}\n")
            print(f"{user[0]};{user[1]}")
    return users

generate_and_save_users()


